package com.daytask.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.daytask.formbean.SonTasktype;
import com.daytask.formbean.TasktypeFrom;
import com.daytask.util.ControllorUtils;
import com.daytask.validate.LoginFormat;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.render.CaptchaRender;
import com.jfinal.plugin.activerecord.Page;

public class DayTaskController extends Controller {
	public static final String KEY = "mykey";
	@SuppressWarnings("rawtypes")
	public Map<String, Object> responseM = new HashMap<String, Object>();// 用来存放返回的数据（json）

	// 生成验证码
	public void imgcode() {
		// 这个是jfinal自带的验证码生成类，可以直接使用的
		CaptchaRender img = new CaptchaRender(KEY);

		render(img);
	}

	public void add() {
		boolean saveResult = false;
		TasktypeFrom form = getModel(TasktypeFrom.class);
		if (StringUtils.isNotBlank(form.getStr("name")) && StringUtils.isNotBlank(form.getStr("caption"))) {
			form.set("userid", ControllorUtils.getUserid());
			saveResult = form.save();
		}
		if (saveResult == true) {
			ControllorUtils.createJsonResultMap(responseM, 1);
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0);
		}
		renderJson(responseM);
	}

	public void delete() {
		String deleteIds = getPara("deleteIds");
		int deleteRecord = 0;
		if (StringUtils.isNotBlank(deleteIds)) {
			String ids[] = StringUtils.split(deleteIds, ",");
			for (String id : ids) {
				List<SonTasktype> list = SonTasktype.dao.find("select id from tb_son_tasktype where tasktypeid =" + id);
				if (list.isEmpty()) {
					if (TasktypeFrom.dao.deleteById(id) == true)
						deleteRecord++;
				}
			}
			if (deleteRecord == ids.length) {
				ControllorUtils.createJsonResultMap(responseM, 1);
			} else if (deleteRecord > 0 && deleteRecord < ids.length) {
				ControllorUtils.createJsonResultMap(responseM, 2);// 部分删除成功
			} else {
				ControllorUtils.createJsonResultMap(responseM, 0); // 删除失败
			}
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0); // 删除失败

		}
		renderJson(responseM);
	}

	public void list() {
		Page<TasktypeFrom> list = TasktypeFrom.dao.paginate(Integer.parseInt( getPara("page")),Integer.parseInt(getPara("rows")), "select * ", "from tb_tasktype");
		 responseM.put("total", list.getTotalRow());
		 responseM.put("rows",list.getList());
		  renderJson(responseM);
	}

	public void edit() {
		boolean result = false;
		TasktypeFrom form = getModel(TasktypeFrom.class);
		int id = form.getInt("id");
		if (StringUtils.isNotBlank(form.getStr("name")) && StringUtils.isNotBlank(form.getStr("caption"))) {
			TasktypeFrom dbObj = form.findById(id);
			if (dbObj != null) {
				dbObj.set("name", form.getStr("name")).set("caption", form.getStr("caption"));
				result =dbObj.update();
				  
			}
		}
		if (result == true) {
			ControllorUtils.createJsonResultMap(responseM, 1);
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0);
		}
		renderJson(responseM);

	}

	public void selection() {
		
		List<TasktypeFrom> list = TasktypeFrom.dao.find("select * from tb_tasktype ");
		renderJson(list);
		
	}
	
	
	
	
	@SuppressWarnings("unchecked")
	@Before({ LoginFormat.class })
	// 先进行格式验证,在验证密码正确性
	// 登陆action
	public void login() {

		responseM.put("state", "success");
		// 返回成功登录的标志
		renderJson(responseM);

	}

}
